//
//  SZRegisterViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZRegisterViewController.h"
#import "SZLoginViewController.h"
#import "PKRequestManager.h"
#import "SZRegisterDetailViewController.h"
#import "SZLoginMessageModel.h"
#import <MJExtension.h>

// 注册的基础网址 返回必要参数
#define kRegisterBaseURL @"http://morguo.com/member.php?mod=register&androidflag=1&appfrom=ios&iosversion=3.13"
// 点击获取验证码
#define kRegisterGetPhoneCodeURL @"http://morguo.com/misc.php?mod=mobileverify&op=v2&getmobilesubmit=1&submitgetmobile=1&androidflag=1&appfrom=ios&iosversion=3.13"

@interface SZRegisterViewController ()
/** 手机号码 */
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
/** 收到的验证码 */
@property (weak, nonatomic) IBOutlet UITextField *verificationCode;
/** 注册获取的基础信息 */
@property (nonatomic, strong) SZLoginMessageModel *registerBaseModel;
/** 获取的手机验证码 */
@property (nonatomic, strong) NSString *registerCode;
@end

@implementation SZRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackItem];// 添加返回Item,返回登录页面
    
    [PKRequestManager requestWithUrlString:kRegisterBaseURL parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);
        _registerBaseModel = [SZLoginMessageModel mj_objectWithKeyValues:dataDic[@"data"]];
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}


/**
 *  添加返回Item
 */
- (void)addBackItem {
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    self.navigationItem.leftBarButtonItem = backItem;
}

/**
 *  返回Item点击事件
 */
- (void)backItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  获取验证码
 */
- (IBAction)getVerificationCode {
    [PKRequestManager requestWithUrlString:kRegisterGetPhoneCodeURL parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);
        _registerCode = dataDic[@"data"][@"smscode"];
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}

/**
 *  注册点击
 */
- (IBAction)registerClick {
    // 输入验证码和获取的不匹配
    if (![_registerCode isEqualToString:_verificationCode.text]) return;
    // 为空
    if (_verificationCode.text.length == 0 || _registerCode.length == 0) return;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"formhash"] = _registerBaseModel.formhash;
    parDic[@"mobilenumber"] = _verificationCode.text;
    [PKRequestManager requestWithUrlString:kRegisterBaseURL parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);
        // 验证成功跳转注册详情页面填写信息
        SZRegisterDetailViewController *detailRegisterVC = [[SZRegisterDetailViewController alloc] init];
        detailRegisterVC.registerBaseModel = _registerBaseModel;
        detailRegisterVC.phoneNumber = _phoneNumber.text;
        [self.navigationController pushViewController:detailRegisterVC animated:YES];
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}

/**
 *  登录点击
 */
- (IBAction)loginClick {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
